#!/usr/bin/perl

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
#
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
#
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI;
use DBI;

use FindBin qw/$Bin/;
use lib "$Bin/lib";

our $se;
require "lib/config.pl";


if (!$ARGV[0]) {
print "No Passwd from Admin\n";
exit;
}

BEGIN {
    require 'func.pm';
}

my $func = func->new();

my $pass_admin = $func->md5crypt( $ARGV[0] ) || die "error crypt passwd\n";


  my $dbh = DBI->connect($se->{dsn},$se->{db_user},$se->{db_pass}, { RaiseError => 1 }) || die $DBI::errstr;
      


  $dbh->do(qq{ CREATE TABLE alias (
  address varchar(255) NOT NULL default '',
  goto text NOT NULL,
  domain varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (address)
) 
                       }) or die $dbh->errstr;
                       


  $dbh->do(qq{ 
  CREATE TABLE  mailbox (
  username varchar(255) NOT NULL default '',
  password varchar(255) NOT NULL default '',
  name varchar(255) NOT NULL default '',
  maildir varchar(255) NOT NULL default '',
  quota int(10) NOT NULL default '0',
  domain varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (username)
) 
                       }) or die $dbh->errstr;
                       
                       
  $dbh->do(qq{ 
  CREATE TABLE  admin_domain (
        idname    INT(11)      NOT NULL,
        m_alias   int(7) not null,
        m_mailbox int(7) not null,
        m_domain int(7) not null,
        mb_domain int(7) not null,
        mb_mailbox int(7) not null,
PRIMARY KEY (idname)
)
                       }) or die $dbh->errstr;




  $dbh->do(qq{ 
  CREATE TABLE  domain (
  idnane int(11) not null,
  domain varchar(255) NOT NULL default '',
  description varchar(255) NOT NULL default '',
  aliases int(10) NOT NULL default '0',
  mailboxes int(10) NOT NULL default '0',
  maxquota int(10) NOT NULL default '0',
  transport varchar(255) default NULL,
  backupmx tinyint(1) NOT NULL default '0',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (domain)
) 
                       }) or die $dbh->errstr;



  $dbh->do(qq{ 
  CREATE TABLE  log_smail (
        action    VARCHAR(20) NOT NULL,
        userid    INT(11)  NOT NULL,
        created   INT(11)  NOT NULL,
        ip        VARCHAR(100) NOT NULL,
        forwarded VARCHAR(100) NOT NULL
) 
                       }) or die $dbh->errstr;
                       
  $dbh->do(qq{ 
  CREATE TABLE  maillog (
        id    INTEGER PRIMARY KEY  AUTOINCREMENT   NOT NULL,
        data  VARCHAR(30) NOT NULL,
        text   text NOT NULL
) 
                       }) or die $dbh->errstr;
                       

   $dbh->do(qq{ 
  CREATE TABLE  mailbox_delete (
  domain varchar(255) NOT NULL default '',
  mailbox varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY (mailbox)
) 
                       }) or die $dbh->errstr;
                    
                    
   $dbh->do(qq{ 
  CREATE TABLE  domain_delete (
  domain varchar(255) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (domain)
) 
                       }) or die $dbh->errstr;
                       
                       
$dbh->do(qq{ 
  CREATE TABLE  users (
        id    INTEGER PRIMARY KEY  AUTOINCREMENT,
        name  VARCHAR(100) NOT NULL,
        pass  VARCHAR(100) NOT NULL,
        info  tinyint(1) NOT NULL,
        created  datetime NOT NULL,
        active  tinyint(1) NOT NULL
) 
}) or die $dbh->errstr;
                       
                       
                       
$dbh->do(qq{
      CREATE TABLE  session (
        session    CHAR(32)     NOT NULL,
        user       INT(11)      NOT NULL,
        time       INT(11)      NOT NULL,
        host       VARCHAR(100) NOT NULL,
        ip         VARCHAR(100) NOT NULL,
        forwarded  VARCHAR(100) NOT NULL,
	PRIMARY KEY (session)
) 
	}) or die $dbh->errstr;
                       


$dbh->do(qq{INSERT INTO users  (id, name, pass, info, active, created) VALUES ( ?, ?, ?, ?, ?, DATETIME('NOW') )}, undef, '4', 'Guest','nopassword', '1', '1'); 
       
$dbh->do(qq{INSERT INTO users (id, name, pass, info, active, created) VALUES ( ?, ?, ?, ?, ?, DATETIME('NOW') )}, undef, '5', 'Admin', $pass_admin,'1','1'); 
       
print "Ok\n";

0;
